import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFrame;

/**
 * This class represents the gamesystem that keeps track of the game's progress
 * This class contains a main method
 * 
 * @author samkim
 *
 */
public class GameSystem {
	private int daysLeft;
	private boolean lose;
	private MainCardPanel mainPanel;
	private Party party;
	private Map map;
	private Shop shop;
	
	public GameSystem() {
		this.shop = new Shop(this);
		this.daysLeft = 100;
		this.map = new Map(this);
		
		this.party = new Party(this);
		this.mainPanel = new MainCardPanel(this);
		
		//Random Event stuff, by Jeoffrey Spinoza
		RandomEventGenerator gen = new RandomEventGenerator(map, mainPanel, party);
		gen.populateMap(.8, .8);
		
	}
	
	public Shop getShop() {
		return this.shop;
	}
	
	public int getDaysLeft() {
		return daysLeft;
	}
	
	public void setDaysLeft(int daysLeft) {
		this.daysLeft = daysLeft;
		this.lose = true;
	}
	
	public boolean getLose() {
		return lose;
	}
	
	public void setLose(boolean lose) {
		this.lose = lose;
	}
	
	/**
	 * decrements day
	 */
	public void decDay() {
		daysLeft--;
		if (daysLeft == 0) {
			this.lose = true;
		}
	}
	
	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public MainCardPanel getMainPanel() {
		return mainPanel;
	}

	public void setMainPanel(MainCardPanel mainPanel) {
		this.mainPanel = mainPanel;
	}

	public Party getParty() {
		return party;
	}

	public void setParty(Party party) {
		this.party = party;
	}

	public MainCardPanel getMainCardPanel() {
		return this.mainPanel;
	}
	
	public void saveGame(){
		try {
			//Create a file and write whatever was returned by the decode method
			BufferedWriter writer=new BufferedWriter(new FileWriter("data.sav"));
			writer.write(this.getParty().getLeader().getName()+"\n");
			writer.write(this.getParty().getLeader().getOccupation().toString()+"\n");
			writer.write(this.getParty().getTravellers().get(0).getName()+"\n");
			writer.write(this.getParty().getTravellers().get(0).getHealth()+"\n");
			writer.write(this.getParty().getTravellers().get(1).getName()+"\n");
			writer.write(this.getParty().getTravellers().get(1).getHealth()+"\n");
			writer.write(this.getParty().getTravellers().get(2).getName()+"\n");
			writer.write(this.getParty().getTravellers().get(2).getHealth()+"\n");
			writer.write(this.getParty().getTravellers().get(3).getName()+"\n");
			writer.write(this.getParty().getTravellers().get(3).getHealth()+"\n");
			writer.write(this.getParty().getAmmunition()+"\n");
			writer.write(this.getParty().getDistance()+"\n");
			writer.write(this.getParty().getFood()+"\n");
			writer.write(this.getParty().getMoney()+"\n");
			writer.write(this.getParty().getWater()+"\n");
			writer.write(this.getParty().getWeight()+"\n");
			writer.write(this.getParty().getCurrent().toString()+"\n");
			writer.write(this.getParty().getCurrent().getLength()+"\n");
			writer.write(this.getParty().getPace().toString()+"\n");
			writer.write(this.getParty().getRations().toString()+"\n");
			writer.write(this.getDaysLeft()+"\n");
			writer.write(this.getParty().getCapacity()+"\n");
			writer.write(this.getParty().getWeapon()+"\n");
			writer.close();
		} catch (IOException exception) {}
		
	}
	
	public void loadGame(){
		Scanner scan=null;
		try {
			scan = new Scanner(new File("data.sav"));
		} catch (FileNotFoundException exception) {
			// TODO Auto-generated catch block
			exception.printStackTrace();
		}
		this.getParty().getTravellers().clear();
		this.getParty().addTraveller(new Member(null));
		this.getParty().addTraveller(new Member(null));
		this.getParty().addTraveller(new Member(null));
		this.getParty().addTraveller(new Member(null));
		this.getParty().getLeader().setName(scan.nextLine());
		String occupationString = scan.nextLine();
		Occupation occupation=null;
		if(occupationString.equals(Occupation.SOLDIER.toString()))
			occupation = Occupation.SOLDIER;
		if(occupationString.equals(Occupation.MEDIC.toString()))
			occupation = Occupation.MEDIC;
		if(occupationString.equals(Occupation.COOK.toString()))
			occupation = Occupation.COOK;
		if(occupationString.equals(Occupation.RANGER.toString()))
			occupation = Occupation.RANGER;
		this.getParty().getLeader().setOccupation(occupation);
		this.getParty().getTravellers().get(0).setName(scan.nextLine());
		this.getParty().getTravellers().get(0).setHealth(Integer.parseInt(scan.nextLine()));
		this.getParty().getTravellers().get(1).setName(scan.nextLine());
		this.getParty().getTravellers().get(1).setHealth(Integer.parseInt(scan.nextLine()));
		this.getParty().getTravellers().get(2).setName(scan.nextLine());
		this.getParty().getTravellers().get(2).setHealth(Integer.parseInt(scan.nextLine()));
		this.getParty().getTravellers().get(3).setName(scan.nextLine());
		this.getParty().getTravellers().get(3).setHealth(Integer.parseInt(scan.nextLine()));
		this.getParty().setAmmunition(Integer.parseInt(scan.nextLine()));
		this.getParty().setDistance(Integer.parseInt(scan.nextLine()));
		this.getParty().setFood(Integer.parseInt(scan.nextLine()));
		this.getParty().setMoney(Integer.parseInt(scan.nextLine()));
		this.getParty().setWater(Integer.parseInt(scan.nextLine()));
		this.getParty().setWeight(Integer.parseInt(scan.nextLine()));
		String locationName = scan.nextLine();
		int locationLength = Integer.parseInt(scan.nextLine());
		Location current = Map.locationSearch(locationName,locationLength);
		this.getParty().setCurrent(current);
		this.getParty().getPace().toString();
		scan.nextLine();
		this.getParty().getRations().toString();
		scan.nextLine();
		this.setDaysLeft(Integer.parseInt(scan.nextLine()));
		this.getParty().setCapacity(Integer.parseInt(scan.nextLine()));
		this.getParty().setWeapon(scan.nextLine());
		for(Member m:this.getParty().getTravellers())
		{
			if(m.getHealth()==0)
				m.setAlive(false);
		}
		
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame("Game");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		GameSystem system = new GameSystem();
		
		frame.getContentPane().add(system.getMainCardPanel());
		frame.pack();
		frame.setVisible(true);
	}
}
